<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;

class Personnel extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 学员列表
     */
    public function index()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['key'])) {
            if(check_mobile($param['key'])){
                $where['mobile'] = ['like', "%" . $param['key'] . "%"];
            }else{
                $where['username'] = ['like', "%" . $param['key'] . "%"];
            }
        }

        $list=model('personnel')->where('personnel_id',['>',0])->where($where)->paginate(config('page_number'));
        $page = input('page')?:0;
        return $this->fetch('index', ['list' =>$list,'page'=>$page]);
    }
    /**
     * 后台添加人才
     */
    public function add(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if($this->insert('personnel', $param,$rule = true, $field = true, $key = 'personnel_id') === true){
                insert_admin_log('添加人才');
                $this->success('添加成功', url('admin/personnel/index'));
            }else{
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('addpersonnel', ['position' => model('Position')->where('status', 1)->select(),'grade'=> model('Grade')->where('status', 1)->select(),'rank'=> model('Rank')->where('status', 1)->select()]);
    }
    /**
     * 编辑学员
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if ($this->update('personnel', $param, input('_verify', true)) === true) {
                insert_admin_log('修改了用户');
                $this->success('修改成功', url('admin/personnel/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('addpersonnel', ['data' => model('personnel')->where('personnel_id', input('id'))->find(),'position' => model('Position')->where('status', 1)->select(),'grade'=> model('Grade')->where('status', 1)->select(),'rank'=> model('Rank')->where('status', 1)->select()]);
    }
    /**
     * 删除学员
     */
    public function del()
    {
        if ($this->request->isGet()) {
            $key = 'personnel_id';
            if ($this->delete('personnel', $this->request->param(),$key ) === true) {
                insert_admin_log('删除了用户');
                $this->success('删除成功', url('admin/personnel/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
    }


    /**
     * 后台批量导入教师
     */
    public function importteacher(){

    }
    /**
     * 后台批量导出教师
     */
    public function exportteacher(){

    }

    /**
     * 人才状态设置
     */
    public function teacherstatus(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if($this->update('personnel', ['personnel_id'=>$param['id'],'status'=>$param['status']], input('_verify', true)) and $this->update('personnel', ['personnel_id'=>$param['id'],'status'=>$param['status']], input('_verify', true),$field = true, $key = 'personnel_id')){
               $this->success('操作成功！', url('admin/personnel/index'));
            } else{
               $this->error($this->errorMsg);
            }
            $this->success('操作成功', url('admin/personnel/index'));
        }
    }


}
